﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Reflection;
using TaskSchedulingSystemForm.Model;
using System.IO;

namespace TaskSchedulingSystemForm.Common
{
    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    public sealed class EnumDescriptionAttribute : Attribute
    {
        private string description;
        public string Description { get { return description; } }

        public EnumDescriptionAttribute(string description)
            : base()
        {
            this.description = description;
        }
    }
    public static class EnumHelper
    {
        public static string GetDescription(Enum value)
        {
            if (value == null)
            {
                throw new ArgumentException("value");
            }
            string description = value.ToString();
            var fieldInfo = value.GetType().GetField(description);
            var attributes = (EnumDescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                description = attributes[0].Description;
            }
            return description;
        }
    }
    enum TSSOP
    {
        [EnumDescription("新建任务")]
        CreateNewTask,
        [EnumDescription("启动任务")]
        StartTask,
        [EnumDescription("启动任务失败")]
        StartTaskFaild,
        [EnumDescription("手动启动任务")]
        StartTaskByHand,
        [EnumDescription("手动启动任务失败")]
        StartTaskByHandFaild,
        [EnumDescription("终止任务")]
        StopTask,
        [EnumDescription("终止任务失败")]
        StopTaskFaild,
        [EnumDescription("修改任务配置信息")]
        ChangeTaskConfigInfo,
        [EnumDescription("删除任务")]
        DeleteTask,
        [EnumDescription("任务正常结束")]
        EndOfTask,
        [EnumDescription("调度系统异常")]
        TSSError
    }  
    class TSSLog
    {    
        private static ILog xtlog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static string taslLogPath = @"D:\tuhc\TSStaskLog\{0}.txt";
        public static void WrriteXTLog(string log)
        {
            xtlog.InfoFormat(log);
        }
        public static void WrriteXTLog(TaskModel task, Enum op)
        {
            xtlog.InfoFormat(string.Format("{0}【{1}】", EnumHelper.GetDescription(op), task.taskName));
        }

        public static void WrriteTaskLog(string logMessage,TaskModel task)
        {
            string logPath = string.Format(taslLogPath, task.taskName);
            FileStream fs1 = new FileStream(logPath, FileMode.Append,FileAccess.Write);
            byte[] data = new UTF8Encoding().GetBytes(logMessage+"\r\n");
            fs1.Write(data, 0, data.Length);
            fs1.Flush();
            fs1.Close();
        }
        public static void WriteTaskLastLog(string logPath, string logMessage)
        {         
            FileStream fs1 = new FileStream(logPath, FileMode.Create, FileAccess.Write);
            byte[] data = new UTF8Encoding().GetBytes(logMessage + "\r\n");
            fs1.Write(data, 0, data.Length);
            fs1.Flush();
            fs1.Close();
        }
        public static void lookTSSLog()
        {
            string txtPath = @"D:\tuhc\TSStaskLog\TSSXTLog.txt";
            System.Diagnostics.Process.Start(txtPath);
        }
    }

}
